<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Goods as ModelGoods;
use app\common\model\Review;

/**
 * 商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * 商品列表
     *
     */
    public function goodsList()
    {
        $get = $this->request->get();
        extract($get);
        $goodsList = ModelGoods::getGoodsList($cate_id ?? 0, $keywords ?? null);
        $this->success('请求成功', $goodsList);
    }
    /**
     * 商品详情
     *
     */
    public function goodsInfo()
    {
        $get = $this->request->get();
        extract($get);
        if (!isset($id)) {
            $this->error('参数错误');
        }
        $goods = ModelGoods::get($id);
        if ($goods)
            $goods['cart_num'] = ModelGoods::getCartNum($id, $this->auth->id);
        $this->success('请求成功', compact('goods'));
    }

    /**
     * 商品评论
     *
     */
    public function goodsReview()
    {
        $get = request()->get();
        extract($get);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $review = new Review();
        $data = $review->with('user')->where('goods_id', $id)->paginate((int)$size ?? 10);
        $this->success('', $data);
    }
}
